package handler

import (
	"car-trade-mgr/cache"
	"car-trade-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarMerAdd(c *gin.Context) {
	carCode := c.PostForm("car_code")
	carName := c.PostForm("car_name")
	carPrice, _ := strconv.Atoi(c.PostForm("car_price"))
	tid, _ := strconv.Atoi(c.PostForm("tid"))
	if carCode == "" || carName == "" || carPrice == 0 || tid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	car := model.CarMer{
		CarCode:  carCode,
		CarName:  carName,
		CarPrice: carPrice,
		Tid:      tid,
	}
	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarTypeAdd(c *gin.Context) {
	typeCode := c.PostForm("type_code")
	typeName := c.PostForm("type_name")
	typeDesc := c.PostForm("type_desc")
	if typeCode == "" || typeName == "" || typeDesc == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	carType := model.CarType{
		TypeCode: typeCode,
		TypeName: typeName,
		TypeDesc: typeDesc,
	}
	err := model.DB.Create(&carType).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func CarMerList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  car_mers.tid AS CarMerId,
		  car_types.type_code AS TypeCode,
		  car_types.type_name AS TypeName,
		  car_types.type_desc AS TypeDesc,
		  car_mers.car_code AS CarCode,
		  car_mers.car_name AS CarName,
		  car_mers.car_price AS CarPrice
		FROM
		  car_mers
		LEFT JOIN car_types ON car_types.id = car_mers.tid;
	*/
	var list []model.CarMerList
	redis_key := "car_mer_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_mers").
			Select("car_mers.tid AS CarMerId",
				"car_types.type_code AS TypeCode",
				"car_types.type_name AS TypeName",
				"car_types.type_desc AS TypeDesc",
				"car_mers.car_code AS CarCode",
				"car_mers.car_name AS CarName",
				"car_mers.car_price AS CarPrice",
			).
			Joins("LEFT JOIN car_types ON car_types.id = car_mers.tid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.CarMer{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func CarMerDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.CarMerList
	redis_key := "car_mer_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_mers").
			Select("car_mers.tid AS CarMerId",
				"car_types.type_code AS TypeCode",
				"car_types.type_name AS TypeName",
				"car_types.type_desc AS TypeDesc",
				"car_mers.car_code AS CarCode",
				"car_mers.car_name AS CarName",
				"car_mers.car_price AS CarPrice",
			).
			Joins("LEFT JOIN car_types ON car_types.id = car_mers.tid").
			Where("car_mers.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
